﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

namespace B10.Models
{
    public class DatabaseRepository : IDatabaseRepository
    {
        B10DataContext _db;

        public DatabaseRepository()
        {
            _db = new B10DataContext();
            DataLoadOptions dl = new DataLoadOptions();
            dl.LoadWith<UserPage>(p => p.UserProfile);
            dl.LoadWith<ForumPost>(p => p.UserProfile);
            dl.LoadWith<Comment>(c => c.UserProfile);
            _db.LoadOptions = dl;
            #if DEBUG
            _db.Log = new DebuggerWriter();
            #endif
        }

        #region IDatabaseRepository Members

        public bool LazyLoading
        {
            get
            {
                return _db.DeferredLoadingEnabled;
            }
            set
            {
                _db.DeferredLoadingEnabled = value;
            }
        }

        public void Add(UserPage page)
        {
            Add<UserPage>(page);
        }

        public UserPage GetPage(int id)
        {
            return _db.UserPages.SingleOrDefault(p => p.Id == id);
        }


        public UserPage GetPageByUrl(string pageUrl)
        {
            return _db.UserPages.SingleOrDefault(p => p.Url == pageUrl);
        }


        public GalleryImage GetGalleryImage(int id)
        {
            return _db.GalleryImages.SingleOrDefault(g => g.Id == id);
        }

        public IQueryable<UserPage> GetAllPages()
        {
            return from p in _db.UserPages
                   orderby p.CreatedDate descending
                   select p;
        }   

        public IQueryable<UserPage> GetActivePages()
        {
            return from p in _db.UserPages
                   where p.Status == PageStatus.Accepted
                   select p;
        }

        public Comment GetComment(int id)
        {
            return _db.Comments.SingleOrDefault(p => p.Id == id);
        }

        public ForumPost GetPost(int id)
        {
            return _db.ForumPosts.SingleOrDefault(p => p.Id == id);
        }

        public UserProfile GetProfile(string userName)
        {
            return (from p in _db.UserProfiles
                    where !p.FacebookId.HasValue && p.UserName == userName
                    select p).SingleOrDefault();
        }

        public UserProfile GetProfile(int? id)
        {
            return (from p in _db.UserProfiles
                    where p.Id == id
                    select p).SingleOrDefault();
        }

        public UserProfile GetFBProfile(long id)
        {
            return (from p in _db.UserProfiles
                    where p.FacebookId == id
                    select p).SingleOrDefault();
        }

        public void Add(GalleryImage img)
        {
            Add<GalleryImage>(img);
        }

        public void Save()
        {
            _db.SubmitChanges();
        }

        public IQueryable<T> GetAll<T>() where T:class
        {
            return _db.GetTable<T>();
        }

        #endregion

        public void Add<TEntity>(TEntity entity) where TEntity : class
        {
            _db.GetTable<TEntity>().InsertOnSubmit(entity);
        }

        public void Delete<TEntity>(TEntity entity) where TEntity : class
        {
            _db.GetTable<TEntity>().DeleteOnSubmit(entity);
        }

    }
}
